//
//  RHInverterParaTitleCell3.m
//  RuiHeAppApp
//
//  Created by Yan Chen on 2024/6/13.
//

#import "RHInverterParaTitleCell.h"
@interface RHInverterParaTitleCell()

@property (nonatomic, strong) NSMutableArray *itemsArray;

@end
@implementation RHInverterParaTitleCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self setupUI];
    }
    return self;
}

-(void)setTitleArr:(NSArray*)titleArr unitArr:(NSArray*)unitArr
{
    _titleArr = titleArr;
    [titleArr enumerateObjectsUsingBlock:^(NSString*  _Nonnull title, NSUInteger idx, BOOL * _Nonnull stop) {
        if (idx < self.itemsArray.count) {
            QMUILabel *titleLab = self.itemsArray[idx];
            NSString *unit = ValidNullData(unitArr[idx]);
            if (unit.length>0) {//有单位
                NSString *content = [NSString stringWithFormat:@"%@ %@",title.rh_localized,unitArr[idx]];
                NSString *key = [NSString stringWithFormat:@" %@",unitArr[idx]];
                titleLab.attributedText = [self attrStringWithContent:content attrKey:key];
            }else{//无单位
                titleLab.text = title.rh_localized;
            }
        }
    }];
    [self updateLabel];
}

-(NSAttributedString*)attrStringWithContent:(NSString *)content  attrKey:(NSString*)key
{
    if(![content containsString:key]){
        return [[NSMutableAttributedString alloc] initWithString:content];
    }
    NSDictionary *attiDic = @{NSFontAttributeName:[UIFont boldSystemFontOfSize:10],NSForegroundColorAttributeName:[UIColor colorWithHexString:@"3F4443" alpha:0.6]};
    
    NSMutableAttributedString *attri_content = [[NSMutableAttributedString alloc] initWithString:content];
    [attri_content setAttributes:attiDic range:[content rangeOfString:key]];
    return attri_content;
}

//重新布局
-(void)updateLabel
{
    [self.itemsArray enumerateObjectsUsingBlock:^(QMUILabel*  _Nonnull lab, NSUInteger idx, BOOL * _Nonnull stop) {
        [lab removeFromSuperview];
    }];
    NSInteger count = self.titleArr.count==1?3:self.titleArr.count;
    //超过3个按3个算
    if (self.titleArr.count) {
        count = 3;
    }
    //左边第一个标题lebel的宽度
    CGFloat titleWidth = 70;
    CGFloat itemtMargin = 5;
    CGFloat labelWidth = (kUI_WINDOW_W - 30 - titleWidth - ((count+1)*itemtMargin))/count;
    
    
    [self.titleArr enumerateObjectsUsingBlock:^(NSString * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if(idx<self.itemsArray.count){
            QMUILabel *lab = self.itemsArray[idx];
            [_bgView addSubview:lab];
            [lab mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.left.mas_offset(itemtMargin+titleWidth+ itemtMargin +(labelWidth+itemtMargin)*idx);
                make.width.offset(labelWidth);
                make.centerY.mas_offset(0);
            }];
        }
    }];
    
}


- (void)setupUI{
    self.contentView.backgroundColor = CFTabViewBgColor;
    UIView *bgView = kView;
    bgView.backgroundColor = UIColor.whiteColor;
    bgView.layer.cornerRadius = 8;
    bgView.layer.masksToBounds = YES;
    bgView.layer.maskedCorners = kCALayerMinXMinYCorner|kCALayerMaxXMinYCorner;
    [self.contentView addSubview:bgView];
    self.bgView = bgView;
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.insets(UIEdgeInsetsMake(0, 0, 0,0));
        make.height.offset(35);
    }];
    

    self.itemsArray = [NSMutableArray array];
    CGFloat bgWidth = MIN(kUI_WINDOW_W, kUI_WINDOW_H)-30;
    __block QMUILabel *lastview;
    //暂且创建5个label,有需要再加
    NSArray *titles = @[@"--",@"--",@"--",@"--",@"--",@"--"];
    CGFloat itemWidth = bgWidth / (titles.count+1);
   
    [titles enumerateObjectsUsingBlock:^(NSString*  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        QMUILabel *titleLab  = [QMUILabel labelWithFont:kSYSTEMFONT(13) textColor:kCOLOR_HEX_A(@"#3F4443", 0.6) textAligment:NSTextAlignmentCenter];
//        titleLab.backgroundColor = [UIColor randomColor];
        titleLab.text = titles[idx];
        titleLab.numberOfLines = 2;
        titleLab.adjustsFontSizeToFitWidth = YES;
        titleLab.minimumScaleFactor = 0.6;
        [bgView addSubview:titleLab];
        [self.itemsArray addObject:titleLab];
        if (lastview) {
            [titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(lastview.mas_right);
                make.centerY.mas_offset(0);
                make.width.offset(itemWidth);
            }];
        }else{
            [titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.mas_offset(itemWidth);
                make.centerY.mas_offset(0);
                make.width.offset(itemWidth);
            }];
        }
        lastview = titleLab;
    }];
    
}

@end
